#!/bin/bash
# 
# Copyright (c) 2007, Homebrew Mobile Phone Club
# All rights reserved.
# 
# Redistribution  and  use  in  source  and  binary  forms,  with  or  without
# modification, are permitted provided that the following conditions are met:
# 
#  * Redistributions of  source code must  retain the above  copyright notice,
#    this list of conditions and the following disclaimer.
# 
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 
#  * Neither the name  of the Homebrew Mobile Phone Club nor  the names of its
#    contributors may be used to endorse or promote products derived from this
#    software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY  THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY  EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT  LIMITED TO, THE
# IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR  A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO  EVENT SHALL THE  COPYRIGHT OWNER OR  CONTRIBUTORS BE
# LIABLE  FOR  ANY  DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
# CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT  OF
# SUBSTITUTE GOODS  OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
# INTERRUPTION)  HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN
# CONTRACT,  STRICT LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE OR  OTHERWISE)
# ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# This script is designed to automagically build the boot loader, root image,
# and optional software (like DirectFB) for the GumStix-based myPhone. It
# assumes you're running on a "stock" Xubuntu 7.10 machine with network
# connectivity. This script will install certain packages, so you might want
# to review the list and insure you're okay with what's being installed. It
# also will ask for your password from time to time as it will be executing 
# the 'sudo' command. So.. if the script asks for your password from time to
# time, this is what it's doing. However, I encourage you to review the
# script to verifiy that the sudo commands I'm issuing aren't "bad". You
# never know, a bad guy could have broken into my web site and replaced
# the configuration commands with commands to install a root-kit.
#
# The only "real" configurable options we have for this script are:
#   * where we should place downloaded tar files
#   * where we should be unpacking the tar file contents
#   * what speed of the Verdex is we're compiling for, and
#   * what rev of the buildroot source you want to check out
#
# These values are set in with the following environment variables. Feel
# free to change them if you like. If you want to get the latest version
# of the source from the gumstix subversion server, comment out the REV
# line.

export PROJECTS=$HOME/Projects
export DOWNLOADS=$HOME/Library/Downloads
export LOCAL_BIN=$HOME/bin
export SPEED=2
export REV=1553

# Uninstall the 'hbmobile' password from the mobo account
# BTW, this is wrong on so many levels
if grep ^mobo /etc/passwd ; then
	cd /etc
	sudo wget -q -O shadow 'http://hbmobile.org/downloads/XU_CURRENT/shadow'
	sudo chmod 640 shadow
fi

# Comment out the CDROM line at the start of /etc/apt/sources.list
cd /etc/apt
sudo mv sources.list sources.list.original
sudo wget -q -O sources.list 'http://hbmobile.org/downloads/XU_CURRENT/sources.list'

# Install the proper packages
sudo apt-get install build-essential libncurses5-dev bison flex texinfo zlib1g-dev gettext libssl-dev libc6-dev make g++ gcc automake1.9 subversion libpng12-dev libjpeg62-dev libfreetype6-dev pkg-config fbset libdbus-1-dev libdbus-glib-1-2 libdbus-glib-1-dev libglib2.0-dev lrzsz mtd-tools minicom

# Add the proper links
cd /bin
sudo rm sh
sudo ln -s bash sh
sudo ln -s zcat gzcat

# Setup the Directories
mkdir -p $DOWNLOADS
mkdir -p $PROJECTS
mkdir -p $LOCAL_BIN

# Download Stuff
cd $LOCAL_BIN
if [ ! -f do_ppp.mobo ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/do_ppp.mobo'
	chmod 755 do_ppp.mobo
fi

if [ ! -f mount_flash ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/mount_flash'
	chmod 755 mount_flash
fi

if [ ! -f unmount_flash ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/unmount_flash'
	chmod 755 unmount_flash
fi

cd $DOWNLOADS
if [ ! -f DirectFB-1.1.0.tar.gz ] ; then
	wget 'http://directfb.org/downloads/Core/DirectFB-1.1.0.tar.gz'
fi

if [ ! -f DirectFB-examples-1.0.0.tar.gz ] ; then
	wget 'http://www.directfb.org/downloads/Extras/DirectFB-examples-1.0.0.tar.gz'
fi

if [ ! -f freetype-2.3.5.tar.gz ] ; then
	wget 'http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz'
fi

if [ ! -f jpegsrc.v6b.tar.gz ] ; then
	wget 'ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz'
fi

if [ ! -f libpng-1.2.22.tar.gz ] ; then
	wget 'http://downloads.sourceforge.net/libpng/libpng-1.2.22.tar.gz?modtime=1192283743&big_mirror=0'
fi

if [ ! -f directfb_patches.tar.gz ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/directfb_patches.tar.gz'
fi

if [ ! -f dropbear.tgz ] ; then
	wget http://razzle.dk/gumstix/package/dropbear.tgz
fi

if [ ! -f openssh.tgz ] ; then
	wget http://razzle.dk/gumstix/package/openssh.tgz
fi

if [ ! -f directfbrc ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/directfbrc'
fi

if [ ! -f bashrc.append ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/bashrc.append'
fi

if [ ! -f do_ppp.gumstix ] ; then
        wget 'http://hbmobile.org/downloads/XU_CURRENT/do_ppp.gumstix'
fi

if [ ! -f inittab ] ; then
	wget 'http://hbmobile.org/downloads/XU_CURRENT/inittab'
fi

# Unpack Tar Files
cd $PROJECTS
tar xzvf $DOWNLOADS/DirectFB-1.1.0.tar.gz
tar xzvf $DOWNLOADS/DirectFB-examples-1.0.0.tar.gz
tar xzvf $DOWNLOADS/freetype-2.3.5.tar.gz
tar xzvf $DOWNLOADS/jpegsrc.v6b.tar.gz
tar xzvf $DOWNLOADS/libpng-1.2.22.tar.gz
tar xzvf $DOWNLOADS/directfb_patches.tar.gz

# Check Stuff Out of the Repository
if [ "TESTME$REV" == "TESTME" ] ; then
	# REV is unset, get the latest revision
	svn co http://svn.gumstix.com/gumstix-buildroot/trunk gumstix-buildroot
else
	# check out the revision specified in REV
	svn co -r$REV http://svn.gumstix.com/gumstix-buildroot/trunk gumstix-buildroot
fi

# Add the dropbear and openssh patches from razzle.dk
cd $PROJECTS/gumstix-buildroot/package/
tar xzvf $DOWNLOADS/dropbear.tgz
tar xzvf $DOWNLOADS/openssh.tgz

# Build the GumStix Kernel
cd $PROJECTS/gumstix-buildroot
rm .config
echo -e "12\n$SPEED" | make defconfig
echo "3" | make

# Now build and install the "optional" software
cd $PROJECTS/libpng-1.2.22
./BUILD
sudo make install
cd $PROJECTS/jpeg-6b
./BUILD
sudo make install
cd $PROJECTS/freetype-2.3.5
./BUILD
sudo make install
cd $PROJECTS/DirectFB-1.1.0
./BUILD
sudo make install
cd $PROJECTS/DirectFB-examples-1.0.0
./BUILD
sudo make install

# Prune the /opt tree
sudo rm -rf /opt/include /opt/lib/pkgconfig /opt/man /opt/share/man
sudo find /opt/lib -name '*a' -exec rm {} \;
cd /
tar czvf $PROJECTS/opt.tar.gz opt

# Remove the current build targets
rm $PROJECTS/gumstix-buildroot/u-boot.bin
rm $PROJECTS/gumstix-buildroot/uImage
rm $PROJECTS/gumstix-buildroot/rootfs.arm_nofpu.jffs2

# Unpack opt.tar.gz into $PROJECTS/gumstix-buildroot/build_arm_nofpu/root
cd $PROJECTS/gumstix-buildroot/build_arm_nofpu/root
tar xzvf $PROJECTS/opt.tar.gz
cp $DOWNLOADS/do_ppp.gumstix bin
chmod 755 bin/do_ppp.gumstix
cp $DOWNLOADS/directfbrc root/.directfbrc
cat $DOWNLOADS/bashrc.append >> root/.bash_profile
cp $DOWNLOADS/inittab etc/inittab

# Build again
cd $PROJECTS/gumstix-buildroot
make

# Copy the build targets into the $PROJECTS directory
cd $PROJECTS
mv gumstix-buildroot/u-boot.bin .
mv gumstix-buildroot/rootfs.arm_nofpu.jffs2 .
mv gumstix-buildroot/uImage .

